package org.example.syn;

/**
 * @author Yyming
 * @version 10.0
 * Created by Yyming on 2020/9/17
 */
public class VolatileTest {
    private static int a = 0, b = 0, x = 0, y = 0;
    public static void main(String[] args) throws InterruptedException {
        int i = 0;
        for (;;) {
            a = 0;b = 0;x = 0;y = 0;
            i++;
            Thread thread = new Thread(() -> {
                shortWart(50000);
                a = 1;
                x = b ;
            });
            Thread thread1 = new Thread(() -> {
                b = 1;
                y = a;
            });
            thread.start();
            thread1.start();
            thread.join();
            thread1.join();
            String result = "x == " + x + " y == " + y + " a == " + a + " b == " + b;
            if (x == 0 && y == 0) {
                System.out.println(result);
                break;
            } else {
                System.out.println(i + "=====>" +result);
            }
        }

    }

    private static void shortWart(long t){
        long start = System.nanoTime();
        long end;
        do {
            end = System.nanoTime();
        } while (start + t >= end);
    }
}
